var vnamePattern=/[\u4e00-\u9fa5\w]{2,10}/;
var phonePattern=/1[345678]\d{9}/;
var passwordPattern=/[\S]{6,12}/;
var register=new Vue({
   el:"#register",
   data:{
      accountDisplay:"block",
      phoneDisplay:"none",
      sendCaptcha:"发送手机号验证码",
      timing:15,
      sender:true,
      vname:"",
      password:"",
      phoneNumber:"",
      confirmPassword:"",
      captcha:"",
      vnameFlag:false,
      passwordFlag:false,
      phoneNumberFlag:false,
      confirmPasswordFlag:false,
      captchaFlag:false,
      vnameError:"",
      passwordError:"",
      phoneNumberError:"",
      confirmPasswordError:"",
      captchaError:"",
      errorInfo:""
   },
   methods:{
      checkVname:function(){
         console.info("blur is working")
         if (vnamePattern.test(this.vname)){
            this.vnameFlag=true;
            this.errorInfo="";
         }else {
            this.vnameFlag=false;
            // alert("用户名以字母，数字，英文，中文，下划线组成，长度为2~10");
            this.errorInfo="用户名以字母，数字，英文，中文，下划线组成，长度为2~10";
         }

      },
      checkPassword:function(){
         if (passwordPattern.test(this.password)){
            this.passwordFlag=true;
            this.errorInfo="";
         }else {
            this.passwordFlag=false;
            // alert("密码需要为非空字符并且长度6到12");
            this.errorInfo="密码需要为非空字符并且长度6到12";
         }
      },
      checkConfirmPassword:function(){
        if (this.confirmPassword!=null&&this.confirmPassword.trim()!==""&&this.confirmPassword===this.password){
           this.confirmPasswordFlag=true;
           this.errorInfo="";
        } else{
           this.confirmPasswordFlag=false;
           this.errorInfo="两次密码输入不一致";
        }
      },
      checkPhoneNumber:function(){
         if (phonePattern.test(this.phoneNumber)){
            this.phoneNumberFlag=true;
            this.errorInfo="";
         }else {
            this.phoneNumberFlag=false;
            // alert("请输入正确的手机号码格式");
            this.errorInfo="请输入正确的手机号码格式";
         }
      },
      toDuanxin:function () {
         this.accountDisplay="none";
         this.phoneDisplay="block";
      },
      toAccount:function () {
         this.accountDisplay="block";
         this.phoneDisplay="none";
      },
      toQQ:function () {
         window.location.href="qqLogin.html?p=2";
      },
      send:function () {
         if (this.phoneNumberFlag&&this.phoneNumber!==null&&this.phoneNumber.trim()!==""){
            console.info("手机号："+this.phoneNumber)
            // var that=this;
            //发送验证码的请求
            axios({
               method:"post",
               url:"/venue/captcha/"+this.phoneNumber
            }).then(res=>{
               console.info(res)
               console.info(res.data.msg)
               if (res.data.code===200){
                  this.sender=false;
                  this.timer();
                  // this.timing = window.setTimeout("timer(this.timing--)",1000);
               }
            }).catch(err=>{
               console.info(err)
            });

         }

      },
      timer:function(){
         var t = 15
         var timecount = setInterval(()=>{
            t--
            this.timing = t
            if(t<0){
               clearInterval(timecount);
               this.sender=true;
            }
         },1000)
      },
      submit:function (n) {
         //登录注册方式：用户名（1）手机号（2）QQ号（3）
         switch (n) {
            case 1:{
               if (this.vnameFlag&&this.passwordFlag&&this.confirmPasswordFlag){
                  axios.post("/venue/register/1",{
                     "vname":this.vname,
                     "pwd":this.password
                  }).then(e=>{
                     console.info(e.data.msg)
                     console.info(e)
                     if (e.data.code===200){
                        alert("注册成功")
                        window.location.href="newVenue.html";
                     }
                     if(e.data.code===300){
                        alert(e.data.msg)
                     }
                  }).catch(err=>{
                     console.info(err)
                  })
               }else{
                  return this.errorInfo="请正确输入数据再注册";
               }
            }//case 1 end
                 break;
            case 2:{
               if (this.phoneNumberFlag&&this.captchaFlag){
                  axios.post("/venue/register/2",{
                     "phoneNumber":this.phoneNumber,
                     "captcha":this.captcha
                  }).then(e=>{
                     console.info(e.data.msg)
                     if (e.data.code===200){
                        alert("注册成功")
                        window.location.href="newVenue.html";
                     }
                  }).catch(err=>{
                     console.info(err)
                  })
               }
            }//case 2 end
                 break;
         }//switch end
      }

   }
});